Advertisement
Guest User

Untitled

a guest
Nov 9th, 2021
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           多列书签
  3. // @include        main
  4. // ==/UserScript==
  5. location.href.startsWith('chrome://browser/content/browser.x') && (() => {
  6.   //按顺序从两列开始
  7.   const column = [
  8.     ['2列显示的书签文件夹名称1', '2列名称2', '2列名称3', '2列名称4'],
  9.     ['3列显示的书签文件夹名称1', '3列名称2'],
  10.     ['4列显示的书签文件夹名称1', '4列名称2'],
  11.     //...由此类推
  12.   ];
  13.  
  14.   const BOOKMARK_WIDTH = 180; // 书签宽度
  15.   const MARGIN_RIGHT = 5;
  16.   const sss = Cc["@mozilla.org/content/style-sheet-service;1"].getService(Ci.nsIStyleSheetService);
  17.   sss.loadAndRegisterSheet(Services.io.newURI("data:text/css," + encodeURIComponent(`
  18.   @-moz-document url("chrome://browser/content/browser.xhtml"){
  19.     #PlacesToolbarItems menuitem.bookmark-item .menu-accel-container {
  20.       display: none !important;
  21.     }
  22.     ${column.map(c => {
  23.       return c.length ? `#PlacesToolbarItems .bookmark-item[container="true"]:is(
  24.         ${c.map(i => `[label=${JSON.stringify(i)}]`).join(', ')}
  25.       ) > menupopup > :is(menuitem, menu, menuseparator) {
  26.         min-width: ${BOOKMARK_WIDTH}px !important;
  27.         max-width: ${BOOKMARK_WIDTH}px !important;
  28.         margin-right: ${MARGIN_RIGHT}px !important;
  29.       }` : '';
  30.     }).join('\n')}
  31.   }`), null, null), sss.USER_SHEET);
  32.  
  33.   function _getScrollableElements () {
  34.     let nodes = this.children;
  35.     if(nodes.length == 1 && nodes[0].className === 'scrollbox-innerbox') {
  36.       nodes = nodes[0].children;
  37.     }
  38.     if (nodes.length == 1) {
  39.       let node = nodes[0];
  40.       if (node.localName == 'children' &&
  41.           node.namespaceURI == 'http://www.mozilla.org/xbl') {
  42.         nodes = document.getBindingParent(this).children;
  43.       } else if (node.localName == 'slot' &&
  44.                  node.namespaceURI == 'http://www.w3.org/1999/xhtml') {
  45.         nodes = node.getRootNode().host.children;
  46.       }
  47.     }
  48.  
  49.     return Array.prototype.filter.call(nodes, this._canScrollToElement, this);
  50.   }
  51.   const PlacesToolbarItems = document.getElementById('PlacesToolbarItems');
  52.   PlacesToolbarItems.addEventListener('popupshowing', (event) => {
  53.     const menupopup = event.target;
  54.     if(menupopup._isInitMultipledBM) return;
  55.     const title = menupopup.parentNode &&
  56.       menupopup.parentNode._placesNode &&
  57.       menupopup.parentNode._placesNode.title;
  58.     menupopup._isInitMultipledBM = true;
  59.     let col = -1;
  60.     if(!title || !menupopup.shadowRoot || (col = column.findIndex(c => {
  61.       return c.indexOf(title) > -1;
  62.     })) === -1) return;
  63.     col += 2;
  64.  
  65.     const markup = `<html:style>
  66.       arrowscrollbox::part(scrollbox), /*bug 1514926*/
  67.       scrollbox.arrowscrollbox-scrollbox{
  68.         overflow-y: auto !important;
  69.       }
  70.       arrowscrollbox > .scrollbox-innerbox{
  71.         display:grid !important;
  72.         grid-template-columns: repeat(${col}, 1fr);
  73.       }
  74.       </html:style>${menupopup.markup.replace(
  75.       /<html:slot\/>|<html:slot><\/html:slot>/,
  76.       '<box class="scrollbox-innerbox" orient="vertical" pack="start" smoothscroll="false" flex="1">$&</box>'
  77.     )}`;
  78.  
  79.     let count = menupopup.shadowRoot.childElementCount;
  80.     menupopup.shadowRoot.prepend(MozXULElement.parseXULToFragment(markup));
  81.     while(count--) menupopup.shadowRoot.lastElementChild.remove();
  82.     const arrowscrollbox = menupopup.shadowRoot.querySelector('arrowscrollbox');
  83.     if(arrowscrollbox && arrowscrollbox._getScrollableElements) {
  84.       arrowscrollbox._getScrollableElements = _getScrollableElements.bind(arrowscrollbox);
  85.     }
  86.  
  87.     if(menupopup.__indicatorBar){
  88.       menupopup.__indicatorBar = null;
  89.       menupopup._scrollBox = null;
  90.     }else{
  91.       menupopup._indicatorBar = menupopup.shadowRoot.querySelector(
  92.         '[part="drop-indicator-bar"]'
  93.       );
  94.       menupopup._scrollBox = menupopup.shadowRoot.querySelector(
  95.         'arrowscrollbox'
  96.       );
  97.     }
  98.  
  99.   }, true);
  100. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement